//
//  YMPDFController.m
//  TestDemo
//
//  Created by fengxijun on 16/6/23.
//  Copyright © 2016年 goappbox. All rights reserved.
//

#import "YMPDFController.h"
#import "YMPDFLoader.h"
#import "YMPDFPageView.h"

@interface YMPDFController ()
@property (nonatomic, strong, readonly) YMPDFLoader *pdfLoader;
@property (nonatomic, strong) YMPDFPageView *pageView;
@end

void pdfDictionaryApplier(const char *  key,
                                    CGPDFObjectRef  value, void * __nullable info) {
    NSLog(@"key : %s",key);
}

@implementation YMPDFController

@synthesize pdfLoader=_pdfLoader;

- (instancetype) initWithContentUrl:(NSURL *)url {
    self = [super init];
    if (self) {
        _url = [url copy];
    }
    return self;
}

- (YMPDFLoader *)pdfLoader {
    if (_pdfLoader == nil) {
        _pdfLoader = [[YMPDFLoader alloc] init];
        __weak typeof(self) weakSelf = self;
        [self.pdfLoader setFileLoadStatus:^(YQJPDFOpenStatus status) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            [strongSelf patchLoadStatus:status];
        }];
    }
    return _pdfLoader;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test1" ofType:@"pdf"]];
    [self.pdfLoader loadContentWithUrl:_url];
}

- (void)patchLoadStatus:(YQJPDFOpenStatus)status {
    switch (status) {
        case YMPDFOpenSuccess:
        {
            [self renderPdf];
        }
            break;
            
        default:
            break;
    }
}

- (void)renderPdf {
//    NSLog(@"catalog : %@",[[self.pdfLoader.pdfDoc catalog] children]);
//    [self.pdfLoader.pdfDoc info];
    [self showPage:1];
}

- (void)showPage:(NSInteger)pageNumber {
    YMPDFPage *page = [self.pdfLoader.pdfDoc pageDataWithPage:pageNumber];
//    NSString *text = page.content.text;
//    NSLog(@"content : %@",page.content.text);
    if (self.pageView == nil) {
        self.pageView = [[YMPDFPageView alloc] initWithFrame:CGRectMake(0.f, 64.f, self.view.frame.size.width, self.view.frame.size.height - 64.f)
                                                        page:page];
        [self.view addSubview:self.pageView];
    } else {
        [self.pageView setPage:page];
    }
    
    if (pageNumber > 1) {
        CATransition *transition = [CATransition animation];
        transition.duration = 0.75;
        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        transition.type=kCATransitionPush;
        
        transition.subtype=kCATransitionFromRight;
        
        transition.delegate = self;
        [self.pageView.layer addAnimation:transition forKey:nil];
    }
}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    static NSInteger i = 2;
    if (i > [self.pdfLoader.pdfDoc totalPageCount]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已经是最后一页了" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        return;
    }
    
    [self showPage:i];
    i++;
    
//    UITouch *touch = [touches anyObject];
//    CGPoint LasttouchPoint =  [touch locationInView:self.view];
//    int LasttouchX = LasttouchPoint.x;
//    startpoint=LasttouchX;
    
//    UITouch *touch = [touches anyObject];
//    CGPoint LasttouchPoint =  [touch locationInView:self.view];
//    int LasttouchX = LasttouchPoint.x;
//    endpoint=LasttouchX;
//    if(startpoint>(endpoint+75)){
//        initialPage++;
//        [self loadPage:initialPage nextOne:YES];
//    } else if((startpoint+75)<endpoint){
//        initialPage--;
//        [self loadPage:initialPage nextOne:NO];
//    }
}

- (CGPDFDocumentRef)getPdfDocument {
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"test1" ofType:@"pdf"];
    CFStringRef pathRef = CFStringCreateWithCString(NULL, [filepath UTF8String], kCFStringEncodingUTF8);
    CFURLRef urlRef = CFURLCreateWithFileSystemPath(NULL, pathRef, kCFURLPOSIXPathStyle, NO);
    CFRelease(pathRef);
    CGPDFDocumentRef documentRef = CGPDFDocumentCreateWithURL(urlRef);
    CFRelease(urlRef);
    return documentRef;
}

@end
